package com.huawei.statistics.utils;


import java.io.FileOutputStream;
import java.net.MalformedURLException;

import cn.xsshome.taip.sign.MD5;
import com.aliyun.oss.common.utils.BinaryUtil;
import com.huawei.statistics.common.Constant;
import com.huawei.statistics.common.service.aliyun.AliyunOSS;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import java.io.IOException;


import java.io.*;
import java.net.URL;
import java.util.Date;

/**
 * Created by 18573 on 2019/1/15.
 */
public class UploadImageOSS {

    //链接url下载图片
    private static String downloadPicture(String imgurl) {
        URL url = null;
        int imageNumber = 0;
        try {
            url = new URL(imgurl);
            DataInputStream dataInputStream = new DataInputStream(url.openStream());
            String imageName = "./"+new Date().getTime() +imgurl.substring(imgurl.lastIndexOf(".",imgurl.length()));
            FileOutputStream fileOutputStream = new FileOutputStream(new File(imageName));
            ByteArrayOutputStream output = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int length;
            while ((length = dataInputStream.read(buffer)) > 0) {
                output.write(buffer, 0, length);
            }
            byte[] context=output.toByteArray();
            fileOutputStream.write(output.toByteArray());
            dataInputStream.close();
            fileOutputStream.close();
            return imageName;
        } catch (MalformedURLException e) {
            e.printStackTrace();
            return null;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    //把本地图片上传到oss
    public static MultipartFile createImg(String path){
        try {
            // File转换成MutipartFile
            FileItem fileItem = createFileItem(path);
            MultipartFile mfile = new CommonsMultipartFile(fileItem);
            return mfile;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return null;
        }
    }

    public static FileItem createFileItem(String filePath) {
        FileItemFactory factory = new DiskFileItemFactory(16, null);
        String textFieldName = "textField";
        int num = filePath.lastIndexOf(".");
        String extFile = filePath.substring(num);
        FileItem item = factory.createItem(textFieldName, "text/plain", true,"MyFileName" + extFile);
        File newfile = new File(filePath);
        int bytesRead = 0;
        byte[] buffer = new byte[10240];
        try{
            FileInputStream fis = new FileInputStream(newfile);
            OutputStream os = item.getOutputStream();
            while ((bytesRead = fis.read(buffer, 0, 10240)) != -1){
                os.write(buffer, 0, bytesRead);
            }
            os.close();
            fis.close();
        }
        catch (IOException e){
            e.printStackTrace();
        }
        return item;
    }


    public static String uploadOSS(String url){
        String path = downloadPicture(url);
        MultipartFile mfile = createImg(path);
        File file = new File(path);
        String md5 = MD5.getMD5(RandCodeUtil.randNumberCodeByCustom("5",10)+new Date().getTime());
        String strUrl = AliyunOSS.uploadFile(Constant.IMG_UPLOAD_TEMP_PATH, md5, mfile);

        if(file.exists()){
            boolean b = file.delete();
            System.out.println("删除文件"+path+b);
        }
        return strUrl;

    }


    public static void main(String[] args) {

        String url = "//pic1.iqiyipic.com/image/20190107/9e/6e/v_112857329_m_601_m1_180_236.jpg";
        String path = uploadOSS("http:"+url);
        System.out.println(path);





    }









}
